<?php

namespace app\common\model;

use app\common\validate\MusicValidate;
use think\Model;
use think\model\concern\SoftDelete;

class MusicModel extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $table = 'mu_music';

    /*
     * 获取多条数据，可以根据歌名和歌手名搜索
     * */
    public function getList($arr)
    {
        $offset = $arr['offset'] ?? 0;
        $limit = $arr['limit'] ?? config('api.limit');
        $wd = $arr['wd'] ?? '';
        $where = [];
        $whereOr = [];
        if(!empty($wd)){
            $where[] = ['muc_name','like',"%$wd%"];
            $whereOr[] = ['singer','like',"%$wd%"];
        }
        $limit = min(config('api.maxlist'),$limit);
        return self::where($where)->whereOr($whereOr)->limit($offset,$limit)->selectOrFail();
    }

    /*
     * 新增一条数据，可以根据歌名和歌手名搜索
     * */
    public function saveOne($arr)
    {
        //验证器验证
        $vali = new MusicValidate();
        $res = $vali->check($arr);
        //写入数据
        if($res !== true){
            return $vali->getError();
        }else{
            $re = self::create($arr);
            if($re){
                return $re;
            }else{
                return false;
            }
        }
    }
}
